package com.jim.director.framework.validation;

import java.util.ArrayList;
import java.util.List;

/**
 * 注解校验结果
 * 封装校验过程中的错误、警告和成功信息
 *
 * @author Jim
 * @date 2024/10/5
 */
public class ValidationResult {

    private boolean valid;
    private List<String> errors;
    private List<String> warnings;
    private List<String> infos;
    private String validatedClass;

    public ValidationResult() {
        this.valid = true;
        this.errors = new ArrayList<>();
        this.warnings = new ArrayList<>();
        this.infos = new ArrayList<>();
    }

    public ValidationResult(String validatedClass) {
        this();
        this.validatedClass = validatedClass;
    }

    /**
     * 添加错误信息（会导致校验失败）
     */
    public void addError(String error) {
        this.errors.add(error);
        this.valid = false;
    }

    /**
     * 添加警告信息（不影响校验结果）
     */
    public void addWarning(String warning) {
        this.warnings.add(warning);
    }

    /**
     * 添加提示信息
     */
    public void addInfo(String info) {
        this.infos.add(info);
    }

    /**
     * 合并另一个校验结果
     */
    public void merge(ValidationResult other) {
        this.errors.addAll(other.getErrors());
        this.warnings.addAll(other.getWarnings());
        this.infos.addAll(other.getInfos());
        if (!other.isValid()) {
            this.valid = false;
        }
    }

    /**
     * 检查是否有错误
     */
    public boolean hasErrors() {
        return !errors.isEmpty();
    }

    /**
     * 检查是否有警告
     */
    public boolean hasWarnings() {
        return !warnings.isEmpty();
    }

    public boolean isValid() {
        return valid;
    }

    public List<String> getErrors() {
        return new ArrayList<>(errors);
    }

    public List<String> getWarnings() {
        return new ArrayList<>(warnings);
    }

    public List<String> getInfos() {
        return new ArrayList<>(infos);
    }

    public String getValidatedClass() {
        return validatedClass;
    }

    public void setValidatedClass(String validatedClass) {
        this.validatedClass = validatedClass;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ValidationResult{");
        sb.append("class=").append(validatedClass);
        sb.append(", valid=").append(valid);
        sb.append(", errors=").append(errors.size());
        sb.append(", warnings=").append(warnings.size());
        sb.append("}");
        return sb.toString();
    }
}

